/*
 * @Author: Chen Blue
 * @Date: 2021-08-10 14:14:40
 * @LastEditTime: 2021-09-07 12:26:54
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \dev-drv\led_drv.c
 */
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/timer.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/serial_core.h>
#include <asm/io.h>
#include <linux/clk.h>
#include <linux/module.h>
#include <linux/version.h>
#include <linux/init.h>

#include <linux/of.h>
#include <linux/of_gpio.h>

#if 0
static int major;
static struct cdev rpi4_led_cdev ;
static struct class *leds_class;
static struct device *leds_class_devs[4];
#endif

#define LEDOFF 				0			/* 关灯 */
#define LEDON 				1			/* 开灯 */

/* gpioled设备结构体 */
struct gpioled_dev{
	dev_t devid;			/* 设备号 	 */
	struct cdev cdev;		/* cdev 	*/
	struct class *class;	/* 类 		*/
	struct device *device;	/* 设备 	 */
	int major;				/* 主设备号	  */
	int minor;				/* 次设备号   */
	struct device_node	*nd; /* 设备节点 */
	int led_gpio;			/* led所使用的GPIO编号		*/
};

struct gpioled_dev gpioled;	/* led设备 */

struct Mazledinfo {
	int pin;
	char * led_name;
};

struct Mazledinfo mazled_info;

static const struct of_device_id rpi_led[] = {
    {
        .compatible = "maz, led",
        .data       = (void *)&mazled_info
    },
    {},
};


static int rpi4_leds_open(struct inode *inode, struct file *file)
{
	file->private_data = &gpioled; /* 设置私有数据 */

	return 0;
}
static ssize_t rpi4_leds_read (struct file *file, char __user *buff, size_t count, loff_t *offp)
{
	return 0;
}
static ssize_t rpi4_leds_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
	int retvalue;
	unsigned char databuf[1];
	unsigned char ledstat;
	struct gpioled_dev *dev = file->private_data;

	retvalue = copy_from_user(databuf, buf, count);
	if(retvalue < 0) {
		printk("kernel write failed!\r\n");
		return -EFAULT;
	}

	ledstat = databuf[0];		/* 获取状态值 */

	if(ledstat == LEDON) {	
		gpio_set_value(dev->led_gpio, 1);	/* 打开LED灯 */
	} else if(ledstat == LEDOFF) {
		gpio_set_value(dev->led_gpio, 0);	/* 关闭LED灯 */
	}
	return 0;
}
static struct file_operations rpi4_leds_fops= {
    .owner = THIS_MODULE,
    .open   =   rpi4_leds_open,     
	.read	=	rpi4_leds_read,	   
	.write	=	rpi4_leds_write,
};

static int led_probe (struct platform_device * pdev)
{
	/* 注册字符设备
     * 参数为主设备号、设备名字、file_operations结构；
     * 这样，主设备号就和具体的file_operations结构联系起来了，
     * 操作主设备为LED_MAJOR的设备文件时，就会调用s3c24xx_leds_fops中的相关成员函数
     * LED_MAJOR可以设为0，表示由内核自动分配主设备号
     */
    /*注册字符设备驱动*/
	int ret;
	printk("[%s][%d]\n",__func__,__LINE__);
#if 0
//获取节点信息的部分实例
	ret = of_property_read_string(pdev->dev.of_node,"mazstr", &str);
	printk("mazstr = %s",str);
	ret = of_property_read_u32(pdev->dev.of_node,"gpios",&value);
	printk("gpio reg = %d",value);
#endif

	gpioled.led_gpio = of_get_named_gpio(pdev->dev.of_node,"gpios",0);
	if(gpioled.led_gpio < 0) {
			printk("can't get led-gpios");
			return -EINVAL;
		}
	printk("led-gpios num = %d\r\n", gpioled.led_gpio);
	
	ret = gpio_direction_output(gpioled.led_gpio, 0);
	if(ret < 0) {
			printk("can't set gpio!\r\n");
	}

	alloc_chrdev_region(&gpioled.devid,0, 1,"rpi-leds");// cat /proc/devices
	gpioled.major = MAJOR(gpioled.devid); /* 获取分配号的主设备号 */
	gpioled.minor = MINOR(gpioled.devid); /* 获取分配号的次设备号 */
	cdev_init(&gpioled.cdev,&rpi4_leds_fops);
	gpioled.cdev.owner = THIS_MODULE;
	cdev_add(&gpioled.cdev,gpioled.devid, 1);
	printk("gpioled major=%d,minor=%d\r\n",gpioled.major, gpioled.minor);	

	gpioled.class = class_create(THIS_MODULE, "myleds");//注册class 让代码在dev自动生成设备

	gpioled.device = device_create(gpioled.class, NULL, gpioled.devid, NULL, "leds"); // /dev/leds
	if (IS_ERR(gpioled.device)) {
		return PTR_ERR(gpioled.device);
	}

	printk(KERN_INFO"raspberry leds initialized\n");
	return  0;
}

static int led_remove (struct platform_device * dev)
{
	/*根据  资源*/
	cdev_del(&gpioled.cdev);
	unregister_chrdev_region(gpioled.devid, 1);
	device_destroy(gpioled.class, gpioled.devid);
	class_destroy(gpioled.class);

	printk(KERN_INFO"leds_exit exit\n");

    /*卸载字符设备驱动*/
	return  0;
}


/* 分配/设置/注册一个 platform_dirve */

static struct platform_driver led_drv={
	.probe  = led_probe,
	.remove = led_remove,
	.driver = {
		.name   = "maziot, rpi_leds",
		.owner = THIS_MODULE,
		.of_match_table = rpi_led
	},
};

static int led_drv_init(void)
{
	printk("[%s][%d]\n",__func__,__LINE__);
	platform_driver_register(&led_drv);
	return 0;
}
static void led_drv_exit(void)
{
	printk("[%s][%d]\n",__func__,__LINE__);
	platform_driver_unregister(&led_drv);
}

module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");
